package com.eastcom.collection

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.util.ArrayList;
import java.util.List;
import com.eastcom.collection.process.*
import com.eastcom.collection.provider.*
import com.eastcom.collection.tools.file.*
import com.eastcom.collection.context.*
public abstract class Application {

    private static Log logger = LogFactory.getLog(Application.class);
	public static final String version = "1.0.0"
    protected String[] args;

    /**
     * ��������������
     *
     * @return
     */
    abstract protected boolean preInitialize();

    /**
     * ��ӡ������������
     */
    abstract protected void printUsage();

    public Application() {

    }

    private void initContext() {
        SpringContextProvider springProvider = new SpringContextProvider();
		PropertyPlaceholderConfigurerProvider.loadProperties()
        try {
            springProvider.setApplicationContext(new FileSystemXmlApplicationContext(ServerContext.springConfigFiles));
			if (SpringContextProvider.getApplicationContext() != null)
			((AbstractApplicationContext) SpringContextProvider.getApplicationContext()).registerShutdownHook();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            logger.error("[E] Application config load error,exit ...");
            System.exit(1);
        }
    }

    /**
     * ��������������
     */
    abstract protected void mainloop();

    /**
     * ����Ӧ�ó�������
     *
     * @param args
     */
    public void run() {
        if (!preInitialize()) {
            logger.error("Initialize application error, exit...");
            System.exit(1);
        }
        initContext();
        mainloop();
    }

    /**
     * ��ֹ����
     */
    public void stop() {
    }

    /**
     * �鿴����״̬
     */
    public void status(String[] args) {
        StringBuffer sb = new StringBuffer();
        if(args.length >= 1){
			if(args[0].equalsIgnoreCase("version")){
				println "当前版本是["+version+"]"
			}
		}
    }

    /**
     * ��ʾ����״̬
     */
    abstract protected ArrayList displayStatus(String command);

    public static ApplicationContext getSpringCtx() {
        return SpringContextProvider.getApplicationContext();
    }

    public String[] getArgs() {
        return args;
    }

    public void setArgs(String[] args) {
        this.args = args;
    }

}